package top.wettis.chat.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import top.wettis.chat.pojo.DO.AskExpert;
import top.wettis.chat.mapper.AskExpertMapper;
import top.wettis.chat.pojo.VO.Result;
import top.wettis.chat.service.IAskExpertService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 专家问答 服务实现类
 * </p>
 *
 * @author wettis
 * @since 2023-04-15
 */
@Service
public class AskExpertServiceImpl extends ServiceImpl<AskExpertMapper, AskExpert> implements IAskExpertService {

    public Result<Object> add(AskExpert askExpert) {
        int id = StpUtil.getLoginIdAsInt();
        //判断有没有同样的问题
        long count = count(new LambdaQueryWrapper<AskExpert>()
                .eq(AskExpert::getQuestion, askExpert.getQuestion()));
        if (count == 0) {
            askExpert.setUserId(id);
            save(askExpert);
            return Result.customInfo(500, "问题已发送请您实时关注", askExpert);
        } else {
            AskExpert expert = getBaseMapper().selectOne(new LambdaQueryWrapper<AskExpert>().
                    eq(AskExpert::getQuestion, askExpert.getQuestion()));
            if (expert.getAnswer() == null) {
                return Result.customInfo(500, "问题有点难，请您容我在思索片刻...", null);
            } else {
                return Result.customInfo(200, "已为您解答", expert);
            }
        }
    }

    public Result<Object> getAll() {

//        List<AskExpert> list = list();
//        List<AskExpert> askExpertList = null;
//        for (AskExpert askExpert : list) {
//            if (askExpert.getQuestion() != null && askExpert.getAnswer() != null) {
//                askExpertList.add(askExpert);
//            }
//        }
        List<AskExpert> askExpertList = getBaseMapper().selectList(new LambdaQueryWrapper<AskExpert>()
                .isNotNull(AskExpert::getQuestion)
                .isNotNull(AskExpert::getAnswer));
        return Result.customInfo(200, "查询成功", askExpertList);
    }
}
